package br.com.wuo.genericInventary.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import br.com.wuo.genericInventary.application.Resources;

import net.miginfocom.swing.MigLayout;

public class configView extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private MigLayout layout;
	private JTextField txtInteval, txtReadtime;
	private JComboBox<String> cmbUSBport;
	private JButton btnSave, btnClear, btnExit;
	private Properties properties;
	
	
	public configView() {
        super("Configurar");
        initialize();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
        addListener();
	}
	
	public void initialize(){
		layout = new MigLayout("","10px[][]10px","");
        setLayout(layout);
        
        try {
			Resources.verifyResources();
			properties = Resources.getProperties();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.exit(1);
		}
        
        txtInteval = new JTextField();
        txtReadtime = new JTextField();
        cmbUSBport = new JComboBox<String>();
        
        cmbUSBport.addItem("COM1");
        cmbUSBport.addItem("COM2");
        cmbUSBport.addItem("COM3");
        cmbUSBport.addItem("COM4");
        cmbUSBport.addItem("COM5");
        cmbUSBport.addItem("COM6");
        cmbUSBport.addItem("COM7");
        
        txtInteval.setText(properties.getProperty("interval.seconds"));
        txtReadtime.setText(properties.getProperty("read.seconds"));
        cmbUSBport.setSelectedItem(properties.getProperty("usb.port"));
        
        btnSave = new JButton("Salvar");
        btnClear = new JButton("Limpar");
        btnExit = new JButton("Sair");
        
        add(new JLabel("Intervalo (segundos)"));
        add(txtInteval, "grow, wrap");
        add(new JLabel("Leitura (segundos)"));
        add(txtReadtime, "grow, wrap");
        add(new JLabel("Porta USB"));
        add(cmbUSBport, "grow, wrap");
        
        add(btnSave, "split 3, span 2, align 50% 50%");
        add(btnClear);
        add(btnExit);
        pack();
        
	}
	
	public void addListener() {
		btnSave.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				properties.setProperty("interval.seconds", txtInteval.getText());
				properties.setProperty("read.seconds", txtReadtime.getText());
				properties.setProperty("usb.port", cmbUSBport.getSelectedItem().toString());
				
				Resources.setProperties(properties);
				clear();
			}
		});
		
		btnClear.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				clear();
				
			}
		});
		
		btnExit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
				
			}
		});
	}
	
	public void clear(){
		txtInteval.setText("");
		txtReadtime.setText("");
		cmbUSBport.setSelectedIndex(-1);
		
	}

}
